/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hellfire.system.graphics;

import hellfire.system.engine.Engine;
import hellfire.system.engine.Main;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;


public class UI extends GLSurfaceView 
{

    public final GLRenderer renderer;

    public UI(Context context) 
    {
        super(context);

        // Create an OpenGL ES 2.0 context.
        setEGLContextClientVersion(2);
        setPreserveEGLContextOnPause(true);
        // Set the Renderer for drawing on the GLSurfaceView
        renderer = new GLRenderer(context);
        setRenderer(renderer);
        setKeepScreenOn(true);
        // Render the view only when there is a change in the drawing data
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        Log.d("R", "UI loaded");
    }

   
    
    @Override
    public boolean onTouchEvent(MotionEvent e) 
    {
        float x = e.getX();
        float y = e.getY();

        x = 2 * x / getWidth() - 1;
        y = 1 - 2 * y / getHeight();
        
        synchronized(Engine.Lock)
        {
	        if(Main.Engine.getCurrentGameScreen() != null)
	        	Main.Engine.getCurrentGameScreen().onTouchEvent(e, new GLRelVec2(x,y));
        }
        return true;
    }
    
    public static boolean testInBox(GLVec2 pos, GLVec2 dim, GLVec2 point)
    {
    	if(pos.x() - dim.x() < point.x() && pos.x() + dim.x() > point.x() && pos.y() + dim.y() > point.y() && pos.y() - dim.y() < point.y())
    		return true;
    	return false;
    }
    
    public static boolean testInRange(GLVec2 pos, float radius, GLVec2 point)
    {
    	float dist = pos.sub(point).length();
    	if(dist <= radius)	return true;
    	return false;
    }
    

}
